package com.mjk.manager.gen.impl.ui;

import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.core.jdbc.annotation.ExampleField;
import com.mjk.common.core.jdbc.annotation.Filed;
import com.mjk.common.core.jdbc.util.provider.SqlProvider;
import com.mjk.common.util.FileUtils;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.StrUtils;
import com.mjk.manager.gen.dto.GenDto;

import java.lang.reflect.Field;
import java.util.List;
/**
 * html 端的vue代码生成
 * @author Administrator
 *
 */
public class HtmlGenerate implements IUiGen{

	@Override
	public boolean isVaild() {
		return false;
	}

	@Override
	public Class<?> createClass(GenDto model) {
		String tableName = getTableName(model);
		String webDir = model.getWebProjectPath()+model.getWebPath()+tableName.toLowerCase()+"\\";
		String listFileName = tableName + "List.html";
		String listM = getTableGenerate(model.getEntity()).replace("#model", tableName);
		FileUtils.write2File(webDir, listFileName, listM);
		return null;
	}
	/**
	 *
     */
	private String getTableGenerate(BaseEntity entity){
		 String tableName = getTableName(entity);
		StringBuilder sb = new StringBuilder();
		sb.append("<!DOCTYPE html>"+WRAP);
		sb.append("<html>"+WRAP);
		sb.append("<head>"+WRAP);
		sb.append("<meta charset=\"utf-8\""+WRAP);
		sb.append("<title></title>"+WRAP);
		sb.append("<link href=\"/base/css/element_ui.css\" rel=\"stylesheet\" type=\"text/css\" >"+WRAP);
		sb.append("<link href=\"/base/css/base.css\" rel=\"stylesheet\" type=\"text/css\" />"+WRAP);
		sb.append("<script src=\"/base/js/utils/jquery-2.0.3.min.js\"></script>"+WRAP);
		sb.append("<script src=\"/base/js/utils/vue.js\" type=\"text/javascript\"></script>"+WRAP);
		sb.append("<script src=\"/base/js/utils/elementui.js\" type=\"text/javascript\"></script>"+WRAP);
		sb.append("<script src=\"/base/js/constns.js\" type=\"text/javascript\"></script>"+WRAP);
		sb.append("<script src=\"/base/js/utils/httpVueLoader.js\" type=\"text/javascript\"></script>"+WRAP);
		sb.append("<script src=\"/base/js/utils/string.js\" type=\"text/javascript\"></script>"+WRAP);
		sb.append("<script src=\"/base/js/utils/page.js\" type=\"text/javascript\"></script>"+WRAP);
		sb.append("<script src=\"/base/js/utils/dateutil.js\" type=\"text/javascript\"></script>"+WRAP);
		sb.append("</head>"+WRAP);
		sb.append("<body>"+WRAP);
		sb.append("<div id=\"app\" v-cloak>"+WRAP);
		sb.append(TAB+"<bee-form :labels=\"form.condition\" :span=8 :buts=\"form.buts\" @customclick=\"formclick\" ></bee-form>"+WRAP);
		sb.append(TAB+"<bee-table ref=\"table\" :title=\"table.title\"  :url=\"table.url\"  :upurl=\"table.upurl\"  :delurl=\"table.delurl\" :page=\"true\" "+WRAP);
		sb.append(TAB_2+":labels=\"table.dialogs\""+WRAP);
		sb.append(TAB_2+":buts=\"table.buts\""+WRAP);
		sb.append(TAB_2+"@customclick=\"customClick\">"+WRAP);
		sb.append(TAB+"</bee-table>"+WRAP);
		sb.append("</div>"+WRAP);
		sb.append("</body>"+WRAP);
		sb.append("<script type=\"module\">"+WRAP);
		sb.append(SPACE+"Vue.use(httpVueLoader);"+WRAP);
		sb.append(SPACE+"new Vue({"+WRAP);
		sb.append(TAB+"el: '#app',"+WRAP);
		sb.append(TAB+"data: {"+WRAP);
		sb.append(TAB+SPACE+"showDialog:false,"+WRAP);
		sb.append(TAB+SPACE+"form:{//查询头配置"+WRAP);
		sb.append(TAB_2+"condition: [//条件选择配置"+WRAP);
        List<Field> fileds = SqlProvider.getValidTableFiled(entity);
		for (Field f : fileds) {
			Filed filed = f.getAnnotation(Filed.class);
    		ExampleField example = f.getAnnotation(ExampleField.class);
    		if(null!=example) {
    			sb.append(getFiledLabel(f));
    		}
		}
		sb.append(TAB_2+"],"+WRAP);
		sb.append(TAB_2+"buts:[//条件按钮"+WRAP);
		sb.append(TAB_2+SPACE+"{name:\"查询\",type:\"success\",commond:\"query\"},"+WRAP);
		sb.append(TAB_2+SPACE+"{name:\"新增\",type:\"warning\",commond:\"add\"},"+WRAP);
		sb.append(TAB_2+SPACE+"{name:\"导出\",type:\"primary\",commond:\"exportfile\"}"+WRAP);
		sb.append(TAB_2+"],"+WRAP);
		sb.append(TAB_2+"result:{}//返回结果"+WRAP);
		sb.append(TAB+SPACE+"},"+WRAP);
		sb.append(TAB+SPACE+"table:{//表单配置"+WRAP);
		StringBuilder dialogs = new StringBuilder(TAB_2+"dialogs:["+WRAP);
		StringBuilder title = new StringBuilder(TAB_2+"title: {"+WRAP);
		for (Field f : fileds) {
			Filed filed = f.getAnnotation(Filed.class);
			String label = "";
			if(null == filed){
				label = f.getName();
			}else{
				label =  ObjectUtils.isEmpty(filed.comment())?f.getName():filed.comment();
			}
    		label = label.replaceAll(RELACE_MATCH, "");
    		dialogs.append(getFiledLabel(f));
    		title.append(TAB_2 + SPACE).append(f.getName()).append(":\" ").append(label).append("\",").append(WRAP);
		}
		dialogs.append(TAB_2+"],//修改新增配置"+WRAP);
		title.append(TAB_2+"},//表头"+WRAP);
		sb.append(dialogs);
		sb.append(title);
		sb.append(TAB_2 + "url: config.url + \"/ld/common/v1/").append(tableName.toLowerCase()).append("?method=get").append(StrUtils.toUpperCaseFirstOne(tableName)).append("List\",//数据获取url").append(WRAP);
		sb.append(TAB_2 + "upurl: config.url + \"/ld/common/v1/").append(tableName.toLowerCase()).append("?method=saveOrUpdate").append(StrUtils.toUpperCaseFirstOne(tableName)).append("\",//数据获取url").append(WRAP);
		sb.append(TAB_2 + "delurl: config.url + \"/ld/common/v1/").append(tableName.toLowerCase()).append("?method=del").append(StrUtils.toUpperCaseFirstOne(tableName)).append("\",//数据获取url").append(WRAP);
		sb.append(TAB_2+"buts:[//自定义操作按钮"+WRAP);
        sb.append(TAB_2+"],"+WRAP);
		sb.append(TAB_2+"row:{}"+WRAP);
		sb.append(TAB+SPACE+"}"+WRAP);
		sb.append(TAB+"},"+WRAP);
		sb.append(TAB+"methods: {"+WRAP);
		sb.append(TAB+SPACE+"getTabelData: function() {//获取数据"+WRAP);
		sb.append(TAB_2+"setTimeout(()=>{"+WRAP);
		sb.append(TAB_3+"this.$refs.table.getData(this.table.url, this.form.result);"+WRAP);
		sb.append(TAB_2+"},100)"+WRAP);
		sb.append(TAB+SPACE+"},"+WRAP);
		sb.append(TAB+SPACE+"formclick:function(row){//查询"+WRAP);
		sb.append(TAB_2+"this.form.result = row;"+WRAP);
		sb.append(TAB_2+"if(row.commond=='query'){"+WRAP);
		sb.append(TAB_3+"this.$refs.table.getData(this.table.url, row);"+WRAP);
		sb.append(TAB_2+"}else if(row.commond=='add'){"+WRAP);
		sb.append(TAB_3+"this.$refs.table.showDialog=true;"+WRAP);
		sb.append(TAB_2+"}else if(row.commond==\"exportfile\"){"+WRAP);
		sb.append(TAB_3 + "let url = config.url+\"/ld/export/v1/").append(tableName.toLowerCase()).append("?method=get").append(StrUtils.toUpperCaseFirstOne(tableName)).append("List\";").append(WRAP);
		sb.append(TAB_3+"exportFile(document,url,row);"+WRAP);
		sb.append(TAB_2+"}else if(row.commond == 'canel'){"+WRAP);
		sb.append(TAB_3+"this.showDialog = false;"+WRAP);
		sb.append(TAB_2+"}"+WRAP);
		sb.append(TAB+SPACE+"},"+WRAP);
		sb.append(TAB+SPACE+"customClick:function(row,commond){//自定义操作按钮回调"+WRAP);
		sb.append(TAB_2+"this.table.row = row;"+WRAP);
		sb.append(TAB+SPACE+"}"+WRAP);
		sb.append(TAB+"},"+WRAP);
		sb.append(TAB+"created: function() {"+WRAP);
		sb.append(TAB+"},"+WRAP);
		sb.append(TAB+"mounted() {"+WRAP);
		sb.append(TAB_2+"this.getTabelData();"+WRAP);
		sb.append(TAB+"},"+WRAP);
		sb.append(TAB+"mixins: [string, page, timeUtils],"+WRAP);
		sb.append(TAB+"components: {"+WRAP);
		sb.append(TAB_2+"\"bee-form\": \"url:/base/components/base/bee-form.vue\","+WRAP);
		sb.append(TAB_2+"\"bee-table\": \"url:/base/components/base/bee-table.vue\","+WRAP);
		sb.append(TAB_2+"\"bee-address\":\"url:/base/components/base/bee-address.vue\""+WRAP);
		sb.append(TAB+"}"+WRAP);
		sb.append("})"+WRAP);
		sb.append("</script>"+WRAP);
		sb.append("</html>"+WRAP);
		return sb.toString();
	}
	/**
	 * 页面展示
     */
	public String getFiledLabel(Field f) {
		Filed filed = f.getAnnotation(Filed.class);
		if(null == filed) {
			return "";
		}
		String label = ObjectUtils.isEmpty(filed.comment())?f.getName():filed.comment();
		label = label.replaceAll(RELACE_MATCH, "");
        return switch (filed.type()) {
            case INPUT -> (TAB_2 + SPACE + "{lable: '" + label + "',key: '" + f.getName() + "',type: 'input'}," + WRAP);
            case DATA -> (TAB_2 + SPACE + "{lable: '" + label + "',key: '" + f.getName() + "',type: 'date'}," + WRAP);
            case TIME -> (TAB_2 + SPACE + "{lable: '" + label + "',key: '" + f.getName() + "',type: 'time'}," + WRAP);
            case TIMEDATA ->
                    (TAB_2 + SPACE + "{lable: '" + label + "',key: '" + f.getName() + "',type: 'datetime'}," + WRAP);
            case SELECT ->
                    (TAB_2 + SPACE + "{lable: '" + label + "',key: '" + f.getName() + "',type: 'select',value:" + filed.selectValue() + "}," + WRAP);
            case SELECT_INPUT ->
                    (TAB_2 + SPACE + "{lable: '" + label + "',key: '" + f.getName() + "',type: 'input-select',url:config.url,valueKey:'',selectValue:{},title:{}}," + WRAP);
            default -> (TAB_2 + SPACE + "{lable: '" + label + "',key: '" + f.getName() + "',type: 'input'}," + WRAP);
        };
	}
}
